// 引入 react 依赖
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { Text, View, Image, TouchableOpacity, ImageBackground, ScrollView, InteractionManager } from 'react-native';

// 引入第三方依赖
import _ from 'lodash';
import pureRender from 'pure-render-decorator';
import { Actions } from 'react-native-router-flux';
import Icon from 'react-native-vector-icons/Ionicons';

// antd 按需加载
import { Tabs, Toast, Portal } from '@ant-design/react-native';

// 引入 处理数据 所需文件
import { myCvLog } from '../../module/module.js'

// 引入样式
import Style from './style/style.js';

// 引入公用组件
import { Status, NavBar, CacheImage, RnFlatList } from '../../components/index.js'

// 引入专用组件
import { } from './components'

// 引入工具包
import { } from '../../util/images.js';
import { press, size, ScreenWidth, hotTextColor, borderBottom, defaultDataList, onEndReached, onRefresh, concatDataList, defaultPage } from '../../util/utils.js'

class CvList extends Component {

  constructor(props) {
    super(props)
    this.state = {
      refreshing: false,
      searchBackgroundColor: '#ccc',
      loading: false,
      dataList: {
        total: 0,
        size: 10,
        current: 1,
        pages: 1,
        records: []
      }
    }
  }

  componentDidMount() {
    InteractionManager.runAfterInteractions(() => {
      this.loading = Toast.loading('加载中')
      this.handleData({
        current: 1,
      })
    });
  }

  handleData(options) {
    const { data } = this.props
    myCvLog({
      size: 10,
      ...options
    }).then(res => {
      Portal.remove(this.loading)

      this.setState({
        dataList: {
          ...res.data,
          records: this.state.dataList.records.concat(res.data.records)
        }
      })
    }).catch(error => {
      console.log(error, 11111)
    }).finally(() => {
      Portal.remove(this.loading)
    })
  }

  onRefresh = () => {
    this.setState({ refreshing: true });
    const { data } = this.props
    const { dataList: { size } } = this.state
    myCvLog({
      current: 1,
      size: 10,
    }).then(res => {
      this.setState({
        dataList: res.data,
        refreshing: false
      })
    }).catch(error => {
      console.log(error, 11111)
    })
  }

  onEndReached = () => {
    const { dataList: { current, size, records, total } } = this.state
    if (records.length < total) {
      this.handleData({
        current: current + 1,
      })
    }
  }

  onScroll = (e) => {
    const { searchBackgroundColor } = this.state
    if (e.nativeEvent.contentOffset.y >= size(366) - 44) {
      this.setState({
        searchBackgroundColor: '#e43130'
      })
    } else {
      this.setState({
        searchBackgroundColor: 'transparent'
      })
    }
  }

  render() {
    const { refreshing, dataList } = this.state
    const { cv } = this.props
    return (
      <View style={Style.container}>
        <Status />
        <NavBar
          title="我的CV"
          back={true}
        />
        <RnFlatList
          style={{
            flex: 1,
            backgroundColor: '#f0f2f5',
          }}
          onScroll={this.onScroll}
          refreshing={refreshing}
          onRefresh={this.onRefresh}
          onEndReached={this.onEndReached}
          removeClippedSubviews={true}
          data={dataList}
          ListHeaderComponent={() => {
            return (
              <View style={{}}>
                <View style={{
                  backgroundColor: '#000000'
                }}>
                  <View style={{
                    justifyContent: 'center',
                    alignItems: 'center',
                    paddingTop: 50,
                    paddingBottom: 80,
                  }}>
                    <Text style={{ fontSize: 14, color: '#fff', lineHeight: 16, marginBottom: 20 }}>当前CV</Text>
                    <Text style={{ fontSize: 28, color: '#fff', lineHeight: 30, }}>{cv.cv}</Text>
                  </View>
                </View>
                <View style={{
                  marginHorizontal: 15,
                  marginTop: -30,
                  marginBottom: 10,
                  backgroundColor: '#fff',
                  borderRadius: 6,
                  overflow: 'hidden',
                  borderWidth: .5,
                  borderColor: '#D9D9D9',
                  flexDirection: 'row',
                  alignItems: 'center',
                }}>
                  <View style={{
                    flex: 1,
                    paddingVertical: 15,
                    alignItems: 'center',
                    justifyContent: 'center',
                  }}>
                    <Text style={{ fontSize: 10, color: '#61676F', lineHeight: 20, }}>累计获得</Text>
                    <Text style={{ fontSize: 12, color: '#61676F', lineHeight: 20, }}>{cv.income}</Text>
                  </View>
                  <View style={{
                    flex: 1,
                    paddingVertical: 15,
                    alignItems: 'center',
                    justifyContent: 'center',
                  }}>
                    <Text style={{ fontSize: 10, color: '#61676F', lineHeight: 20, }}>累计使用</Text>
                    <Text style={{ fontSize: 12, color: '#61676F', lineHeight: 20, }}>{cv.pay}</Text>
                  </View>
                  <View style={{
                    flex: 1,
                    paddingVertical: 15,
                    alignItems: 'center',
                    justifyContent: 'center',
                  }}>
                    <Text style={{ fontSize: 10, color: '#61676F', lineHeight: 20, }}>剩余</Text>
                    <Text style={{ fontSize: 12, color: '#61676F', lineHeight: 20, }}>{cv.cv}</Text>
                  </View>
                </View>
              </View>
            )
          }}
          renderItem={({ item }) => {
            if (item.type == 1) {
              return (
                <View style={{
                  flexDirection: 'row',
                  alignItems: 'center',
                  justifyContent: 'space-between',
                  padding: 15,
                  ...borderBottom()
                }}>
                  <View style={{}}>
                    <Text style={{ fontSize: 14, color: '#333', lineHeight: 16, marginBottom: 10 }}>{item.desc}</Text>
                    <Text style={{ fontSize: 14, color: '#999', lineHeight: 16, }}>{item.createTime}</Text>
                  </View>
                  <Text style={{ fontSize: 14, color: '#61676F', lineHeight: 16, }}>+{item.cv}</Text>
                </View>
              )
            }
            if (item.type == 2) {
              return (
                <View style={{
                  flexDirection: 'row',
                  alignItems: 'center',
                  justifyContent: 'space-between',
                  padding: 15,
                  ...borderBottom()
                }}>
                  <View style={{}}>
                    <Text style={{ fontSize: 14, color: '#333', lineHeight: 16, marginBottom: 10 }}>{item.desc}</Text>
                    <Text style={{ fontSize: 14, color: '#999', lineHeight: 16, }}>{item.createTime}</Text>
                  </View>
                  <Text style={{ fontSize: 14, color: '#61676F', lineHeight: 16, }}>-{item.cv}</Text>
                </View>
              )
            }
            return null
          }}
        />
        <View style={{ paddingHorizontal: 15, paddingVertical: 10 }}>
          <TouchableOpacity
            activeOpacity={0.9}
            onPress={() => Actions.withdrawal()}
            style={{
              paddingVertical: 14,
              paddingHorizontal: 20,
              backgroundColor: hotTextColor,
              borderRadius: 0,
              alignItems: 'center',
              justifyContent: 'center',
              borderRadius: 5
            }}
          >
            <Text style={{ fontSize: size(28), color: '#000', lineHeight: size(30), }}>提现</Text>
          </TouchableOpacity>
        </View>
      </View >
    );
  }
}


export default connect(({ user, cv }) => ({
  user, cv
}))(pureRender(CvList))
